﻿#region Copyright(C) 2011 NCS Pte. Ltd. All rights reserved.

// ==============================================================================
// Copyright(C) 2011 NCS Pte Ltd
//
// SYSTEM NAME			: MOH Online Collaboration Portal
// COMPONENT ID			: NCS.OCP.SharePointComponent.Layouts.SharePointComponent.upload
// COMPONENT DESC		: upload the medias to album.
//
// CREATED DATE/BY	    : 10-08-2011 / Zhang Zu Meng
//
// REVISION HISTORY     :
// DATE/BY  ISSUE#/SR#/CS/PM#/OTHERS    DESCRIPTION OF CHANGE
// 
// ==============================================================================

#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;

namespace NCS.OCP.SharePointComponent.Layouts.SharePointComponent
{
    public partial class upload : System.Web.UI.Page
    {
        public DataTable sessionKeys
        {
            get
            {
                DataTable dt = new DataTable();
                DataColumn dc = new DataColumn();
                dc.ColumnName = "fileType";
                dt.Columns.Add(dc);

                DataColumn dc3 = new DataColumn();
                dc3.ColumnName = "fileTypeName";
                dt.Columns.Add(dc3);

                DataColumn dc4 = new DataColumn();
                dc4.ColumnName = "filePath";
                dt.Columns.Add(dc4);

                DataColumn dc5 = new DataColumn();
                dc5.ColumnName = "orgFileName";
                dt.Columns.Add(dc5);

                return dt;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Files.Count > 0)
            {
                int chunk = Request["chunk"] != null ? int.Parse(Request["chunk"]) : 0;
                string fileName = Request["name"] != null ? Request["name"] : string.Empty;

                string uploadPath = System.Configuration.ConfigurationManager.AppSettings["uploadPath"].ToString();
                uploadPath = Context.Server.MapPath(uploadPath);
                if (uploadPath.EndsWith("\\") == false)
                {
                    uploadPath = uploadPath + "\\";
                }

                DataTable dt = sessionKeys;
                DataRow row = dt.NewRow();

                HttpPostedFile file = Request.Files[0];
                string currentTransactionGuid = Request["currentGuid"];

                string fileTypeName = file.FileName.Substring(file.FileName.LastIndexOf(".") + 1).ToLower();
                string typesPic = "jpg|jpeg|gif|tif|bmp|png";

                try
                {
                    //file.SaveAs(uploadPath + file.FileName);
                    #region TODO later
                    if (typesPic.IndexOf(fileTypeName) > -1)
                    {
                        using (var fs = new FileStream(Path.Combine(uploadPath, fileName), chunk == 0 ? FileMode.Create : FileMode.Append))
                        {
                            var buffer = new byte[file.InputStream.Length];
                            file.InputStream.Read(buffer, 0, buffer.Length);

                            fs.Write(buffer, 0, buffer.Length);
                            fs.Close();
                            fs.Dispose();
                        }
                        if (chunk == 0)
                        {
                            row["fileType"] = "pic";
                            row["filePath"] = uploadPath + fileName;
                            row["fileTypeName"] = fileTypeName;
                            row["orgFileName"] = file.FileName;
                            dt.Rows.Add(row);
                        }
                    }
                    #endregion

                    #region log filedetails
                    //row["fileType"] = "pic";
                    //row["filePath"] = uploadPath + fileName;
                    //row["fileTypeName"] = fileTypeName;
                    //row["orgFileName"] = file.FileName;
                    if (chunk == 0)
                    {
                        string logFileName = currentTransactionGuid.Replace("-", "") + ".txt";
                        if (File.Exists(uploadPath + logFileName))
                        {
                            StreamWriter sw = new StreamWriter(uploadPath + logFileName, true);
                            sw.WriteLine(row["fileType"].ToString() + "?" + row["filePath"].ToString() + "?" + row["fileTypeName"].ToString() + "?" + row["orgFileName"].ToString());
                            sw.Close();
                            sw.Dispose();
                        }
                        else
                        {
                            StreamWriter sw = File.CreateText(uploadPath + logFileName);
                            sw.WriteLine(row["fileType"].ToString() + "?" + row["filePath"].ToString() + "?" + row["fileTypeName"].ToString() + "?" + row["orgFileName"].ToString());
                            sw.Close();
                            sw.Dispose();
                        }
                    }
                    #endregion
                }
                catch (Exception ex)
                {
                    if (JM.JCY.Common.JCYUtility.ExceptionHandle("save files", ex) == false)
                    {
                        throw ex;
                    }
                }

            }
        }
    }
}